package com.crm.reports;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.log4j.Logger;

import com.crm.beans.QuotParts;


public class JRInvoice {
	
	private final static Logger logger=Logger.getLogger(JRInvoice.class.getName());
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String refno="KK/CP/1/19-12-2011";
		  Map parameters = new HashMap();
		  parameters.put("htitle", "Invoice");
		  parameters.put("invoiceno", "KK/CP/1/12-09-2012");
		  parameters.put("sonumber", "1234");
		  parameters.put("sodate", "12/06/2011");
		  parameters.put("billto", "ECIL");
		  parameters.put("shipto", "KK Circuits And Equipments");
		  parameters.put("terms", "KK Circuits And Equipments");
		  parameters.put("tinno", "KK Circuits And Equipments");
		  parameters.put("cstno", "KK Circuits And Equipments");
		  parameters.put("panno", "KK Circuits And Equipments");
		  parameters.put("terms", "1 Vat @4% EXTRA.\n" +
				  "2 Validity: 30DAYS \n" +
				  "3 Waranty: NOT APPLICABLE\n" +
				  "4 Payment:  100% within 15 Days from the date of receipt of material at your stores,else interest @ 2.5%/month needs to be paid by your organisation\n" +
				  "5  Prices are valid for quoted quantities only.\n" +
				  "6  Quoted lead time is as of now only.\n" +
				  "7 Order should be placed on: KK Circuits & equipments\n"+
				  "8 Order should be placed on: KK Circuits & equipments\n"+
				  "9 Order should be placed on: KK Circuits & equipments\n"+
				  "10 Order should be placed on: KK Circuits & equipments\n"+
				  "11 Order should be placed on: KK Circuits & equipments\n"+
				  "12 Order should be placed on: KK Circuits & equipments\n"+
				  "13 Order should be placed on: KK Circuits & equipments\n"+
				  "14 Order should be placed on: KK Circuits & equipments\n"+
				  "15 Order should be placed on: KK Circuits & equipments\n"+
				  "16 Order should be placed on: KK Circuits & equipments\n"+
				  "17 Order should be placed on: KK Circuits & equipments\n"+
				  "18 Order should be placed on: KK Circuits & equipments\n"+
				  "19 Order should be placed on: KK Circuits & equipments\n"+
				  "20 Order should be placed on: KK Circuits & equipments\n"+
				  "21 Order should be placed on: KK Circuits & equipments\n"+
				  "22 Order should be placed on: KK Circuits & equipments\n"+
				  "23 Order should be placed on: KK Circuits & equipments\n"+
				  "24 Order should be placed on: KK Circuits & equipments\n"+
				  "25 Order should be placed on: KK Circuits & equipments\n"+
				  "26 Order should be placed on: KK Circuits & equipments\n"+
				  "27 Order should be placed on: KK Circuits & equipments\n"+
				  "28 Order should be placed on: KK Circuits & equipments\n"+
				  "29 Order should be placed on: KK Circuits & equipments\n"+
				  "30 Order should be placed on: KK Circuits & equipments\n"+
				  "31 Order should be placed on: KK Circuits & equipments\n"+
				  "32 Order should be placed on: KK Circuits & equipments\n"+
				  "33 Order should be placed on: KK Circuits & equipments\n"+
				  "34 Order should be placed on: KK Circuits & equipments\n"+
				  "35 Order should be placed on: KK Circuits & equipments\n"+
				  "36 Order should be placed on: KK Circuits & equipments\n"+
				  "37 Order should be placed on: KK Circuits & equipments\n"+
				  "38 Order should be placed on: KK Circuits & equipments\n"+
				  "39 Order should be placed on: KK Circuits & equipments\n");
		  parameters.put("from", "KK Circuits And Equipments");
		  parameters.put("manager", "Manager Operations");
		  parameters.put("subtotal", "10000");
		  parameters.put("vat", "1000");
		  parameters.put("total", "10000");
		  parameters.put("priceword", "ten thousand rupees only");
		  
		  List<QuotParts> l=new ArrayList<QuotParts>();
		  
		  QuotParts qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="dshjsdghjsdghsdgfghdsfghsdgfhdghfghsdgfhdjdhfjhjkfhjdshfjsdjfjsdfjsdjfjsdfjhd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		  
		  qp=new QuotParts();
		  qp.totalcost="1000";
		  qp.sno="1";
		  qp.partNo="er2323";
		  qp.desc="abcd";
		  qp.make="honda";
		  qp.qty="10";
		  qp.unitPrice="10";
		  l.add(qp);
		  
		runReport("D:/work/crmWeb/WebContent/WEB-INF/classes/reports/Invoice.jasper",refno, parameters, l.toArray());
		
		}
	
	public static void runReport(String inname, String in,Map<String, String> parms, Object[] prtArry) throws JRException, IOException {
		long start = System.currentTimeMillis();
		 
		JasperReport techreport = (JasperReport) JRLoader.loadObject(inname);

		
		JasperPrint print = JasperFillManager.fillReport(techreport,parms,new JRBeanArrayDataSource(prtArry));
		 OutputStream output=new FileOutputStream(new File("invoice.pdf"));
		 
		JasperExportManager.exportReportToPdfStream(print,output);
		
		output.close();
		
		Desktop.getDesktop().open(new File("invoice.pdf"));
		System.err.println("Filling time : " + (System.currentTimeMillis() - start));
	}
	public static void runPIReport(String inname, String in,
			Map<String, String> parms, Object[] prtArry) throws JRException, IOException {
		long start = System.currentTimeMillis();
		JasperPrint print=JasperFillManager.fillReport(inname, parms,new JRBeanArrayDataSource(prtArry));
		
		Calendar currentDate = Calendar.getInstance();
		  SimpleDateFormat formatter=new SimpleDateFormat("MMM-yyyy");
		  String dt = formatter.format(currentDate.getTime());
		  boolean exists = (new File("proforma invoices//"+dt)).exists();
		  if (!exists) {
			  try{
				  (new File("proforma invoices//"+dt)).mkdir();
			  }catch(Exception e){
				  JOptionPane.showMessageDialog(null, "Unable To Create The Folder :"+dt);
				  logger.info("Unable To Create The Folder :"+dt);
				  e.printStackTrace();
			  }
			  logger.info("CREATED A FOLDER "+dt);
		  } else {
		   logger.info(dt+" Folder Already Exists");
		  }
		  
		String inv="";
		String[] temp=in.split("/");
		for(int i=0;i<temp.length;i++){
			inv+=temp[i]+"_";
		}
		OutputStream output=new FileOutputStream(new File("proforma invoices//"+dt+"//"+inv+"_proformainvoice.pdf"));
		JasperExportManager.exportReportToPdfStream(print,output);
		output.close();
		Desktop.getDesktop().open(new File("proforma invoices//"+dt+"//"+inv+"_proformainvoice.pdf"));
		System.err.println("Filling time : " + (System.currentTimeMillis() - start));
	}
}
